String clazzTpl({package}) => """
import 'package:dio/dio.dart';
import 'package:dio/adapter.dart';
import 'package:$package/api/transformer/transformer.dart';
import 'package:$package/api/interceptors/log_interceptors.api.dart';
import 'package:$package/api/interceptors/request_interceptors.api.dart';
import 'package:$package/api/interceptors/response_interceptors.api.dart';

/// 请叫我code哥
/// Generates Dart code
class HttpManager {
  static HttpManager _instance = HttpManager._internal();
  static Dio? _dio;

  factory HttpManager({required String host}) => _instance._host(host);

  HttpManager._internal() {
    if (null == _dio) {
      _dio = Dio(BaseOptions(
        contentType: Headers.jsonContentType,
        responseType: ResponseType.json,
        connectTimeout: 30000,
        receiveTimeout: 30000,
      ));
      _dio!.interceptors.add(RequestInterceptors());
      _dio!.interceptors.add(ResponseInterceptors());
      _dio!.interceptors.add(LogInterceptors());
      (_dio!.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
          (client) {
        // config the http client
//        client.findProxy = (uri) {
//          //proxy all request to localhost:8888
//          return "PROXY localhost:8888";
//        };
        // you can also create a new HttpClient to dio
        // return HttpClient();
      };
    }
  }

  //用于指定特定域名
  HttpManager _host(String host) {
    if (_dio != null && _dio!.options.baseUrl != host) {
      _dio!.options.baseUrl = host;
    }
    return this;
  }

  Future<Response<dynamic>> request<T>(
    String path, {
    dynamic data,
    required Map<String, dynamic> queryParameters,
    required Options options,
    CancelToken? cancelToken,
    ProgressCallback? onSendProgress,
    ProgressCallback? onReceiveProgress,
  }) async {
//    if (options.extra != null &&
//        options.extra!.containsKey('isLoading') &&
//        options.extra!['isLoading']) {
//      Toast.showLoading();
//    }
    Response resp;
    try {
      resp = await _dio!.request(path,
          data: data,
          queryParameters: queryParameters,
          cancelToken: cancelToken,
          options: options,
          onSendProgress: onSendProgress,
          onReceiveProgress: onReceiveProgress);
    } on DioError catch (e) {
      resp = Response(
          requestOptions: e.requestOptions,
          statusCode: 500,
          statusMessage: e.message,
          data: e);
    }
    if (!(resp.data is DioError) &&
        T.toString() != 'dynamic' &&
        T.toString() != 'void') {
      resp.data = await MyTransformer<T>().transformer(resp.data);
    }
    if (T.toString() == 'dynamic' && resp.data is String) {
      resp.data = await MyTransformer<T>().transformerString(resp.data);
    }
//    if (resp.requestOptions.extra.containsKey('isLoading') &&
//        resp.requestOptions.extra['isLoading']) {
//      Toast.closeLoading();
//    }
    // resp.data is DioError or BaseEntity<T> or dynamic
    return resp;
  }
}
""";
